<?php
/**
 * Product attributes model. Attribute it is something that charactarize product 
 * (i.e. screen size for TV sets, size for T-shirts etc)
 * 
 * @author Konstantin Shamko <konstantin.shamko@gmail.com> 
 * @version 0.0.1
 * @copyright  Copyright (c) 2009 Konstantin Shamko
 * @package Products Module
 * @license  New BSD License
 *
 */
class Models_ProductAttributes extends Bel_Model_Abstract  {

	/**
	 * Array of tables' classes which are in one-to-many relation with products' attributes table.  
	 *
	 * @var array
	 */	
	protected $_dependentTables = array('Models_ProductAttributes_DefaultValues','Models_ProductTemplates_Attributes');
	
	/**
	 * Sets product attributes table name and primary key
	 *
	 * @return void
	 */	
	public function __construct() {
		$this->_name = TABLE_PREFIX . "product_attributes";
		$this->_primary = array('attribute_id');
		parent::__construct ();
	}
	
	/**
	 * Returns attribute with default value. Default values are defined if attribute 
	 * field type is SELECT or RADIO
	 *
	 * @param string $where - where clause
	 * @param string $order - order clause
	 * @param int $page - page number to select
	 * @return array - array with stdObjects
	 */	
	public function getAll($where = NULL, $order = NULL, $page = 1){
		$attributes = parent::getAll($where, $order, $page); 		
		$oAttrVal = new Models_ProductAttributes_DefaultValues();		
		foreach($attributes as $attribute){
			$attribute->default_values = $oAttrVal->fetchAll('attribute_id='.$attribute->attribute_id);
		}		
		return $attributes;
	}
	
	/**
	 * Returns attribute with it default values by ID. Default values are defined if attribute 
	 * field type is SELECT or RADIO
	 *
	 * @param integer $id - attribute ID
	 * @return array 
	 */
	public function getEntryById($id) {
		$attribute = parent::getEntryById($id);
		
		if($attribute){			
			$attr = $attribute->toArray();			
			$attr['default_values'] = $attribute->findDependentRowset('Models_ProductAttributes_DefaultValues')->toArray();
		}
		
		unset($attribute);
		
		return $attr;
	}	
}
